import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'
// import {new_router} from '@/utils/menu'


Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'layout',
    component: () => import ('@/views/layout.vue'),
    redirct:'home',
     children:[
        // ...new_router
        {
          path: '/home',
          name: 'home',
          component: () => import ('@/views/child/home.vue')
        },
        //   user-info
        {
          path: '/user-info',
          name: 'user-info',
          component: () => import ('@/views/child/user-info.vue')
        },
        // user-avatar
        {
          path: '/user-avatar',
          name: 'user-avatar',
          component: () => import ('@/views/child/user-avatar.vue')
        },
        //  user-pwd
       {
         path: '/user-pwd',
         name: 'user-pwd',
         component: () => import ('@/views/child/user-pwd.vue'),
   
       },
      //  art-cate
      {
        path: '/art-cate',
        name: 'art-cate',
        component: () => import ('@/views/child/art-cate.vue'),
  
      },
      // art-list
      {
        path: '/art-list',
        name: 'art-list',
        component: () => import ('@/views/child/art-list.vue'),
  
      },

     ]

  },
  {
    path: '/login',
    name: 'login',
    component: () => import ('@/views/login.vue')
  },
  {
    path: '/reg',
    name: 'reg',
    component: () => import ('@/views/reg.vue')
  },
  {
    path: '/404',
    name: '404',
    component: () => import ('@/views/404.vue'),
   
  },
  
  
  
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to,from,next)=>{
  
  store.dispatch('getuserinfo')
  if(to.path=='/login' || to.path=='/reg' ) return next()
  else{
    const tokenStr=localStorage.getItem('token')
    if(!tokenStr){
      return next('/login')
    }
    next()
  }
})

export default router
